<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="活动名称" prop="collectName">
        <el-input
          v-model="queryParams.collectName"
          placeholder="请输入活动名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择活动状态"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.activity_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动开始时间" prop="timeStart">
        <el-date-picker
          v-model="queryParams.timeStart"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="timeEnd">
        <el-date-picker
          v-model="queryParams.timeEnd"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['activity:gather:query']"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:gather:add']"
          >新增集客活动</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple.length === 0"
          @click="handleDelete(null)"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['activity:gather:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="gatherList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
        align="center"
      /> -->
      <el-table-column
        min-width="140"
        label="活动名称"
        align="center"
        prop="collectName"
        show-overflow-tooltip
      />
      <el-table-column
        min-width="80"
        label="活动状态"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          {{ scope.row.status | typeFilter(dict.type.activity_status) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="活动门店"
        align="center"
        prop="useStores"
      />
      <el-table-column
        min-width="160"
        label="活动开始时间"
        align="center"
        prop="timeStart"
        sortable
      />
      <el-table-column
        min-width="160"
        label="活动结束时间"
        align="center"
        prop="timeEnd"
        sortable
      />
      <el-table-column
        min-width="140"
        label="虚拟报名人数"
        align="center"
        prop="inventedEnrollNumber"
      />
      <el-table-column
        min-width="140"
        label="报名人数"
        align="center"
        prop="enrollNumber"
      />
      <el-table-column
        min-width="160"
        label="创建时间"
        align="center"
        prop="createTime"
        sortable
      />
      <el-table-column
        min-width="140"
        label="创建人"
        align="center"
        prop="createBy"
      />
      <el-table-column
        min-width="140"
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handDetail(scope.row)"
            >详情</el-button
          >
          <!-- 1 -->
          <el-dropdown
            v-if="scope.row.status === 1"
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleDelete"
                icon="el-icon-key"
                v-hasPermi="['activity:gather:remove']"
                >删除</el-dropdown-item
              >
              <el-dropdown-item
                command="handleStart"
                icon="el-icon-key"
                v-hasPermi="['activity:gather:start']"
                >启用</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <!-- 2 -->
          <el-dropdown
            v-else-if="scope.row.status === 2"
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleSuspend"
                icon="el-icon-key"
                v-hasPermi="['activity:gather:suspend']"
                >暂停</el-dropdown-item
              >
              <el-dropdown-item
                command="handleShare"
                icon="el-icon-key"
                v-hasPermi="['activity:gather:share']"
                >分享</el-dropdown-item
              >
              <el-dropdown-item command="handleActive" icon="el-icon-key"
                >活动数据</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <!-- 3 -->
          <el-dropdown
            v-else-if="scope.row.status === 3"
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleEnable"
                icon="el-icon-key"
                v-hasPermi="['activity:gather:enable']"
                >启用</el-dropdown-item
              >
              <el-dropdown-item
                command="handleEnd"
                icon="el-icon-key"
                v-hasPermi="['activity:gather:end']"
                >结束</el-dropdown-item
              >
              <el-dropdown-item command="handleActive" icon="el-icon-key"
                >活动数据</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <!-- 4 -->
          <el-dropdown
            v-else
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleCopy"
                icon="el-icon-key"
                v-hasPermi="['activity:gather:copy']"
                >复制活动</el-dropdown-item
              >
              <el-dropdown-item command="handleActive" icon="el-icon-key"
                >活动数据</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 分享 -->
    <el-dialog
      :title="shareDialog.title"
      :visible.sync="shareDialog.openShare"
      width="680px"
      append-to-body
    >
      <div :style="{ textAlign: 'center' }">
        <qrCode ref="qrCode" :url="shareDialog.shareUrl" />
      </div>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="downLoad">下载小程序活动码</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCollectList,
  deleteCollectById,
  collectExport,
  collectUpdateState,
} from '@/api/activity/gather'
import MmvUploadImage from '../components/mmv-upload-image.vue'

export default {
  name: 'Tenant',
  dicts: ['activity_status'],
  components: {
    MmvUploadImage,
  },
  data() {
    return {
      shareDialog: {
        title: '活动小程序分享',
        openShare: false,
        shareUrl: '',
      },
      showSearch: true,
      // 表格遮罩
      loading: false,
      // 表格数据
      gatherList: [],
      multiple: [],
      total: 1,
      // 导出遮罩层
      exportLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collectName: null,
        status: null,
        timeStart: null,
        timeEnd: null,
      },
    }
  },
  filters: {
    typeFilter(val, type) {
      if (val === null) return null
      let data = type.filter((item) => {
        return val == item.value
      })
      if (data.length) {
        return data[0].label
      } else {
        return val
      }
    },
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询租户管理列表 */
    getList() {
      this.loading = true
      getCollectList(this.queryParams).then((response) => {
        this.gatherList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        tenantId: null,
        tenantName: null,
        collectName: null,
        tenantConcat: null,
        tenantPhone: null,
        tenantFlag: 0,
        tenantType: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        roleKeys: [],
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.multiple = selection.map((item) => item.id)
    },
    /** 详情按钮操作 */
    handDetail(row) {
      this.$router.push({ path: '/gather/detail/' + row.id })
    },
    /** 操作事件 */
    handleCommand(val, row) {
      switch (val) {
        case 'handleSuspend':
          // 暂停
          this.handleConfirm({ msg: '确定要暂停活动吗？' }, (e) => {
            this.handleStart(row, 3)
          })
          break
        case 'handleShare':
          // 分享
          this.handleShare(row)
          break
        case 'handleActive':
          // 活动数据
          this.$router.push({ path: '/gather/active/' + row.id })
          break
        case 'handleDelete':
          // 删除
          this.handleDelete(row)
          break
        case 'handleStart':
          // 启用
          this.handleConfirm({ msg: '确定要启用活动吗？' }, (e) => {
            this.handleStart(row, 2)
          })
          break
        case 'handleCopy':
          // 复制
          this.$router.push({ path: '/gather/copy/' + row.id + '/copy' })
          break
        case 'handleEnable':
          //启用
          this.handleConfirm({ msg: '确定要启用活动吗？' }, (e) => {
            this.handleStart(row, 2)
          })
          break
        case 'handleEnd':
          //结束
          this.handleConfirm(
            { msg: '确定要结束活动，结束活动后无法恢复？' },
            (e) => {
              this.handleStart(row, 4)
            }
          )
          break
        default:
          console.log('操作类型不正确')
      }
      console.log(val, row)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/gather/add' + '/add' })
    },
    // 更新状态
    handleStart(row, status) {
      collectUpdateState({ id: row.id, status: status }).then((res) => {
        this.$message.success(res.msg)
        this.getList()
      })
    },
    /** 分享操作 */
    handleShare(row) {
      this.shareDialog.openShare = true
      this.shareDialog.shareUrl = row.shareUrl
    },
    /** 下载操作 */
    downLoad() {
      this.$refs.qrCode.downLoad('集客活动码')
    },
    /** 各种状态弹窗操作 */
    handleConfirm(data, callback) {
      this.$modal.confirm(data.msg).then((e) => {
        callback()
      })
    },
    /** 删除操作 */
    handleDelete(row) {
      let message, ids
      if (row) {
        message =
          '是否确认删除集客活动名称为“' + row.collectName + '”的数据项吗？'
        ids = row.id
      } else {
        message = '是否确认删除集客活动数据项吗？'
        ids = this.multiple.join(',')
      }
      this.$modal.confirm(message).then((e) => {
        deleteCollectById({ id: ids }).then((res) => {
          this.$message.success(res.msg)
          this.getList()
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = {
        collectName: this.queryParams.collectName,
        status: this.queryParams.status,
        timeStart: this.queryParams.timeStart,
        timeEnd: this.queryParams.timeEnd,
      }
      this.$modal
        .confirm('是否确认导出所有集客活动数据项？')
        .then(() => {
          this.exportLoading = true
          collectExport(queryParams)
            .then((res) => {
              this.$download.name(res.msg)
              this.exportLoading = false
            })
            .catch((err) => {
              this.exportLoading = false
              console.log(err)
            })
        })
        .catch(() => {})
    },
  },
}
</script>
